package com.snack.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.snack.common.result.Result;
import com.snack.entity.pojo.Batch;
import com.snack.entity.pojo.Snack;
import com.snack.entity.pojo.User;
import com.snack.mapper.BatchMapper;
import com.snack.service.BatchService;
import com.snack.service.StoreService;
import com.snack.service.UserService;
import com.snack.utils.UserIdThreadLocal;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author huangfuhao
 * @Date 2025/3/28
 */
@Service
@Slf4j
public class BatchServiceImpl extends ServiceImpl<BatchMapper, Batch> implements BatchService {

    @Resource
    private UserService userService;

    @Resource
    private StoreService storeService;

    @Override
    public Result addBatch(Batch batch) {
        log.info("新增批次：{}", batch);
        Long storeId = userService.lambdaQuery().eq(User::getId, UserIdThreadLocal.getUserId()).one().getStoreId();
        LocalDate now = LocalDate.now();
        batch.setStoreId(storeId);
        batch.setPurchaseDate(now);
        batch.setState(0);
        save(batch);
        return Result.success(batch);
    }

    @Override
    public Result listBatch(Batch batch) {
        Long storeId = storeService.getStoreIdByUser();
        // 根据店铺id获取其拥有的所有批次
        LambdaQueryWrapper<Batch> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Batch::getStoreId, storeId);

        if (batch.getBatchNumber() != null) {
            // 如果号不为空，则做模糊查询
            queryWrapper.like(Batch::getBatchNumber, batch.getBatchNumber());
        }

        if (batch.getPurchaseDate() != null) {
            // 如果采购日期不为空，则做等值查询
            queryWrapper.eq(Batch::getPurchaseDate, batch.getPurchaseDate());
        }
        if (batch.getState() != null){
            queryWrapper.eq(Batch::getState, batch.getState());
        }

        List<Batch> list = list(queryWrapper);

        if (list == null || list.isEmpty()) {
            return Result.success(new ArrayList<>());
        }

        return Result.success(list);
    }


    @Override
    public Result deleteBatch(Batch batch) {
        if (batch.getId() == null) {
            return Result.error("批次ID不能为空");
        }

        boolean isDeleted = removeById(batch.getId());
        if (isDeleted) {
            return Result.success("删除成功");
        } else {
            return Result.error("删除失败");
        }
    }

    @Override
    public Result updateBatch(Batch batch) {
        log.info("更新批次信息：{}", batch);

        // 根据批次ID查询批次是否存在
        Batch existingBatch = getById(batch.getId());
        if (existingBatch == null) {
            return Result.error("批次不存在");
        }

        // 更新批次号
        if (batch.getBatchNumber() != null) {
            existingBatch.setBatchNumber(batch.getBatchNumber());
        }

        // 更新采购日期
        if (batch.getPurchaseDate() != null) {
            existingBatch.setPurchaseDate(batch.getPurchaseDate());
        }

        // 更新批次信息
        boolean update = updateById(existingBatch);
        if (!update) {
            return Result.error("更新失败");
        }
        return Result.success(existingBatch);
    }

}
